/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Handlers;

import apiAccess.APIAccess;
import apiAccess.APIEXception;
import apiAccess.Items;
import java.io.IOException;
import java.util.List;
import org.xml.sax.SAXException;

/**
 *
 * @author Kevin
 */
public class ItemsHandler {
    List<Items> itemsList;
    int from;
    int to;
    int cant;
    private int page;

    public ItemsHandler(int cant, int category_id, int subcategory_id, int languageId) throws SAXException, IOException, APIEXception{
        this.cant=cant;
        this.from=0-cant;
        this.to=0;
        page=0;
        if(subcategory_id==-1){
            itemsList=APIAccess.getCategItemsList(category_id, languageId);
            }else{
            itemsList=APIAccess.getSubCategItemsList(category_id, subcategory_id, languageId);
            }
    }
    public ItemsHandler(int cant, String name) throws APIEXception, IOException, SAXException{
        this.cant=cant;
        this.from=0-cant;
        this.to=0;
        page=0;
        itemsList=APIAccess.getProductListByName(name);
    }
    

   public List<Items> next(){
       if(from+cant>=itemsList.size()){
           return null;
       }
       page++;
       from+=cant;
       if(to+cant>=itemsList.size()){
            to=itemsList.size();
       }else{
           to+=cant;
       }
       List<Items> ret= itemsList.subList(from, to);
       return ret;
   }

   public List<Items> back(){
       if(from==0){
           return null;
       }
       page--;
       to-=to-from;
       from-=cant;
       List<Items> ret= itemsList.subList(from, to);
       return ret;
   }

   public boolean activoNext(){
       if(from+cant>=itemsList.size()){
           return false;
       }
       return true;

   }

    public boolean activoBack(){
       if(from==0){
           return false;
       }
       return true;

   }

    public int cantItems() {
        if(hayItems())
            return itemsList.size();
        return 0;
    }

    public boolean hayItems(){
        return itemsList!=null;
    }

    public int pages() {
        int aux=itemsList.size()%this.cant;
        int resp=(int)(itemsList.size()/this.cant);
        if(aux!=0){
            resp++;
        }
        return resp;
    }

    public String pagesFoot(){
        return page+"/"+pages();
    }
}
